<?php

namespace app\data\logic;

use app\data\model\AdModel;
use app\data\model\AdPositionModel;
use app\data\model\CaseCategoryModel;
use app\data\model\CaseModel;
use app\data\model\CmsCatModel;
use app\data\model\CmsLabelModel;
use app\data\model\CmsModel;
use app\data\model\CmsPageModel;
use app\data\model\CmsPagePartModel;
use app\data\model\ConfigModel;
use app\data\model\DownloadCategoryModel;
use app\data\model\DownloadModel;
use app\data\model\NavigateModel;
use app\data\model\ProCategoryModel;
use app\data\model\QuestionCategoryModel;
use app\data\model\QuestionModel;
use app\data\model\TagsLibModel;
use app\data\service\TagsLibService;
use think\db\Query;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Log;

class TagsLibLogic extends BaseLogic
{
    protected TagsLibService $tagsLibService;

    public function __construct()
    {
        $this->tagsLibService = new TagsLibService();
    }

    public function getList($pageIndex, $pageSize, $cat = 1, $condition = null, $search = null): array
    {
        $where=[];
        try {
            if ($search) {
                $keywords = $search['keywords'] ?? null;
                if ($keywords) {
                    $where[] = function ($query) use ($keywords) {
                        $query->whereLike('name', '%' . $keywords . '%');
                    };
                }
            }


            if ($condition) {
                if (array_key_exists('is_show', $condition)) {
                    $where[] = function ($query) use ($condition) {
                        $query->where('is_show', $condition['is_show']);
                    };
                }
            }


            $fields = "*";
            $order = 'order desc,create_time desc';
            return $this->tagsLibService->getPageList($pageIndex, $pageSize, $where, $fields, $order, []);
        } catch (\Exception $exception) {
            Log::debug($exception->getMessage());
            Log::debug($exception->getFile() . ":" . $exception->getLine());
            return [];
        }
    }
    public function getAll(): array
    {
        $where=[];
        try {
            $fields = "*";
            $order = 'order desc,create_time desc';
            return $this->tagsLibService->getAll($fields,['is_show'=>1], $order, []);
        } catch (\Exception $exception) {
            Log::debug($exception->getMessage());
            Log::debug($exception->getFile() . ":" . $exception->getLine());
            return [];
        }
    }
    public function createTags($data): array
    {
        try {
            $uuid=bin2hex(random_bytes(16));
            $data['tags_key']=$uuid;
            $data['is_show']=1;
            $ret = $this->tagsLibService->insert($data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (ValidateException $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }
    public function getTagsDetail($id): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $res = $this->tagsLibService->getOne('*', $where, []);
            if ($res) {
                return $this->responseInfo(true, 'SUCCESS', $res);
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (ValidateException $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }
    public function deleteCms($id, $checkData = ''): array
    {
        try {
            $where = [
                'id' => $id
            ];
            $item = $this->tagsLibService->getOne("*", $where);
            $ret = $this->tagsLibService->softDel($where);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (ValidateException $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }
    public function updateTagsLib($id, $data, $checkData): array
    {
        try {
            //TODO 二期开发
            $where = [
                'id' => $id
            ];
            $item = $this->tagsLibService->getOne("*", $where);
            $ret = $this->tagsLibService->update($where, $data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (ValidateException $e) {
            Log::error($e->getMessage());
            return $this->responseInfo(false, $e->getMessage());
        }
    }
}